package com.njtoyo.taxi.admin.excel;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mdf
 * @title: 导出乘客
 * @description: TODO
 * @date 2024/11/25 14:20
 */
public class 导出乘客 {

    public static void main(String[] args) {
        String result = HttpRequest.get("https://taxi-online.njydi.cn/apis/business/customer?cantonId=7&date=&current=1&size=1000")
                .header("authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIzIiwianRpIjoiY2NiYmQ3YThmMDk4NGNlZWE0NDU3ODkzNTk5NTQ3YzkiLCJleHAiOjE3MzI1MTgwNDd9.i-VkJzIwfBtbyUvMQ5D-LHkl0l4TztbHbAJI5TAJUzw")
                .execute().body();
        System.out.println(JSON.parseObject(result));
        JSONArray jsonArray = JSON.parseObject(result).getJSONObject("data").getJSONArray("records");
        List<Map<String, Object>> rows = new ArrayList<>();
        for (Object obj : jsonArray) {
            Map<String, Object> row = new LinkedHashMap<>();
            JSONObject jsonObject = (JSONObject) obj;
            row.put("编号", jsonObject.getString("id"));
            row.put("手机号", jsonObject.getString("mobileNumber"));
            row.put("乘客名称", jsonObject.getString("name"));
            row.put("注册时间", DateUtil.date(jsonObject.getLong("createdAt")).toString());
            row.put("账户状态", jsonObject.getBoolean("isActive")?"正常":"禁用");
            row.put("账户渠道", "小程序");

            rows.add(row);
        }

        ExcelWriter writer = ExcelUtil.getWriter("/Users/saber/Desktop/customer.xlsx");
        writer.write(rows, true);
        writer.close();
    }
}
